<?php
/**
 * Booking Model for RoomBooking Component
 *
 * @version    1.0
 * @package    Joomla
 * @subpackage RoomBooking
 * @copyright (C) 2011 Giovanni Marzulli
 * @license	   GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');


class RoomBookingModelBooking extends JModel
{
	/**
	 * Constructor that retrieves the ID from the request
	 *
	 * @access	public
	 * @return	void
	 */
	function __construct()
	{
		parent::__construct();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}

	/**
	 * Method to set the identifier
	 *
	 * @access	public
	 * @param	int identifier
	 * @return	void
	 */
	function setId($id)
	{
		// Set id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}

	/**
	 * Method to get a booking record
	 * @return object with data
	 */
	function &getData()
	{
		// Load the data
		if (empty( $this->_data )) {
			$query = ' SELECT r.id, r.short_description, r.description, date_format( r.start, "%Y-%m-%d %H:%i" ) as start, date_format( r.end, "%Y-%m-%d %H:%i" ) as end, '.
					' r.notes, r.repeat_type, r.repeat_until, r.id_user, r.id_room , r.series, room.name AS room_name,r.total  FROM #__roombooking_bookings AS r LEFT OUTER JOIN '.
					' #__roombooking_rooms AS room ON r.id_room = room.id  WHERE r.id = '.$this->_id;
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
			$this->_data->short_description = null;
			$this->_data->description = null;
			$this->_data->start = null;
			$this->_data->end = null;
			$this->_data->notes = null;
			$this->_data->repeat_type = null;
			$this->_data->repeat_until = null;
			$this->_data->series = null;
			$this->_data->id_user = JFactory::getUser()->id;
			$this->_data->id_room = null;
			$this->_data->room_name = null;
			$this->_data->total = null;
			
		}
		return $this->_data;
	}
	
	
	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store()
	{	
		$row =& $this->getTable();

		$data = JRequest::get( 'post' );

		// Bind the form fields to the booking table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Make sure the booking record is valid
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Sets the error message
		if (!$row->store()) {
			$this->setError( $row->getErrorMsg() );
			return false;
		}
		return $row->id;
	}
	
	
	/**
	 * Method to delete record(s)
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function delete($cids)
	{
		if(!isset($cids))
			$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

		$row =& $this->getTable();

		if (count( $cids )) {
			foreach($cids as $cid) {
				if (!$row->delete( $cid )) {
					$this->setError( $row->getErrorMsg() );
					return false;
				}
			}
		}
		return true;
	}
	
	/**
	 * Method to delete a bookings series
	 *
	 */
	function deleteSeries($series){
		$query='DELETE FROM #__roombooking_bookings WHERE series='.$series;
		$this->_db->setQuery( $query );
		return $this->_db->query($query);
	}
	
	/**
    * Gets booking ids of a room
    * @return array Array of objects containing the data from the database
    */
	function bookingCidsOfRoom($id_room)
	{
		$query = ' SELECT id  '
        	   . ' FROM #__roombooking_bookings WHERE id_room='.$id_room;
		$this->_db->setQuery( $query );
		$cids = $this->_db->loadResultArray();
		
		$this->delete($cids);
	}
	
	
	
}